/********************************************************************************
* File Name:	addnewhouse.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "addnewhouse.h"
#include "ui_addnewhouse.h"
#include <QMessageBox>
#include <QtSql>
#include <QSqlQuery>
#include "databaseconnection.h"

Addnewhouse::Addnewhouse(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Addnewhouse)
{
    ui->setupUi(this);
}

Addnewhouse::~Addnewhouse()
{
    delete ui;
}

void Addnewhouse::on_btnreset_clicked()
{
    this->close();
}

void Addnewhouse::on_btnsave_clicked()
{
    if(ui->leoid->text()==""||ui->lehid->text()==""||ui->leprice->text()=="")
    {
        QMessageBox::about(this,"Message","item with * can't not be empty!");
        return;
    }
    QString roomId = ui->lehid->text().trimmed();
    QString renterId = ui->leoid->text().trimmed();
    QString type = ui->letype->text().trimmed();
    QString location = ui->lelocation->text().trimmed();
    QString floor = ui->lefloor->text().trimmed();
    QString ratingNum = ui->letype_3->text().trimmed();
    QString realNum = ui->letype_2->text().trimmed();
    QString area = ui->learea->text().trimmed();
    QString price = ui->leprice->text().trimmed();
    QString air= "0";
    QString phone= "0";
    QString tv = "0";
    QString washer = "0";
    QString internet = "0";
    QString fridge = "0";
    QString remark=ui->lenote->text().trimmed();

    int iratNum = ratingNum.toInt();
    int irealNum = realNum.toInt();

    if(ui->checkBox->isChecked())
    {
        tv = "1";
    }
    if(ui->checkBox_2->isChecked())
    {
        fridge = "1";
    }
    if(ui->checkBox_3->isChecked())
    {
         washer = "1";
    }
    if(ui->checkBox_4->isChecked())
    {
        air = "1";
    }
    if(ui->checkBox_5->isChecked())
    {
        phone ="1";
    }
    if(ui->checkBox_6->isChecked())
    {
        internet = "1";
    }
    bool success=false;
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        this->close();
    }
    if(irealNum>iratNum)
    {
        QMessageBox::about(this,"Message","real num can't not bigger than rating num");
        databaseconnection::Close();
        this->close();
    }


    QSqlQuery query;
    query.exec("select count(*) from house.renter where renterId='"+renterId+"'");
    int renterNum=0;
    if(query.next())
    {
        renterNum = query.value(0).toInt();
    }
    if(renterNum==0)
    {
        QMessageBox::about(this,"Message","No this renter,Please add renter first!");
        databaseconnection::Close();
        this->close();
    }
    int iroomId = roomId.toInt();

    QString maxNum;
    query.exec("select count(*) from house.roomInfo where roomId='"+roomId+"'");
    if(query.next())
    {
        iroomId = query.value(0).toInt();
    }
    if(iroomId!=0)          //存在这个ｉｄ
    {
        query.exec("select  roomId from roomInfo order by roomId desc");
        if(query.next())
        {
              maxNum=query.value(0).toString();
        }
        QMessageBox::about(this,"Message","max roomId="+maxNum);
        databaseconnection::Close();
        this->close();
    }
    QString cmd = "insert into house.roomInfo values('"+roomId+"','"+renterId+"','"+type+"','"+location+"','"+floor+"','"+ratingNum+"','"+realNum+"','"+area+"','"+price+"','"+air+"','"+phone+"','"+tv+"','"+washer+"','"+internet+"','"+fridge+"','"+remark+"')";
    query.exec(cmd);
    if(query.isActive())
    {
        query.numRowsAffected();
        QMessageBox::about(this,"message","add success");
    }
    databaseconnection::Close();
    this->close();
}
